package com.lngsyz.file.config;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "com.lngsyz.file.mapper.source1",sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSource1Config {
	
	@Value("${spring.datasource.druid.db1.url}")
    private String url;
    @Value("${spring.datasource.druid.db1.username}")
    private String username;
    @Value("${spring.datasource.druid.db1.password}")
    private String password;
    @Value("${spring.datasource.druid.db1.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.druid.db1.initial-size}")
    private int initialSize;
    @Value("${spring.datasource.druid.db1.min-idle}")
    private int minIdle;
    @Value("${spring.datasource.druid.db1.max-active}")
    private int maxActive;
    @Value("${spring.datasource.druid.db1.max-wait}")
    private int maxWait;
    @Value("${spring.datasource.druid.db1.validation-query}")
    private String validationQuery;
    @Value("${spring.datasource.druid.db1.time-between-eviction-runs-millis}")
    private long timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.druid.db1.min-evictable-idle-time-millis}")
    private long minEvictableIdleTimeMillis;
    @Value("${spring.datasource.druid.db1.test-on-borrow}")
    private boolean testOnBorrow;
    @Value("${spring.datasource.druid.db1.test-on-return}")
    private boolean testOnReturn;
	
    @Bean(name = "dataSource1")
    @ConfigurationProperties("spring.data-source1")
    public DruidDataSource oracleDataSource(){
    	DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(this.url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setValidationQuery(validationQuery);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        System.out.println("db1 init");
        return datasource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/source1/*.xml"));
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "source1TransactionManager")
    public DataSourceTransactionManager oracleTransactionManager(){
        return new DataSourceTransactionManager(oracleDataSource());
    }
    
    /*
    * 解决druid 日志报错：discard long time none received connection:xxx
    * */
    @PostConstruct
    public void setProperties(){
        System.setProperty("druid.mysql.usePingMethod","false");
    }
}
